﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Tool.UserControls;

namespace ERP_RORZE_TOOL.Tool.Forms
{
	public partial class frm_ExchangeRateAdd : DevExpress.XtraEditors.XtraForm
	{
		private ucExchangeRate parent;
		public frm_ExchangeRateAdd(ucExchangeRate fExchangeRate)
		{
            InitializeComponent();
            btAdd.Image = ERP_RORZE_TOOL.Properties.Resources.add;
            btCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
			parent = fExchangeRate;
            this.CancelButton = this.btCancel;
            this.ActiveControl = txtExchangerRate;
			
		}

		private void btAdd_Click(object sender, EventArgs e)
		{
			ExchangeRateHelper helper = new ExchangeRateHelper(new ExchangeRates());
			if (GetData(helper.ValueObject))
			{

				if (helper.Insert() > 0)
				{
					if (Utilities.MessageConfirmationYesNo(ToolConstants.Message.AddSuccessAndQuestion))
				    {

				    txtExchangerRate.Text = "";
                    }
                    else
                    {
                        this.Close();
                    }
				}
				else
				{
                    Utilities.MessageErrorOk(ToolConstants.Message.AddFailed);
				}
				parent._action = 0;
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
		#region Private methods

		private bool GetData(ExchangeRates o)
		{
			#region Check valid

			if(Utilities.IsNullOrEmpty(txtExchangerRate.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.ExchangeRate.ExchangeRateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			#endregion

			o.Rate = decimal.Parse(txtExchangerRate.Text);
		    
			return true;
		}
		
		#endregion
	}
}